package com.gzsxy.esjy.service.auth.controller.api;

import com.gzsxy.esjy.service.auth.service.AddressService;
import com.gzsxy.esjy.service.base.dto.request.AddressRequest;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.service.base.dto.response.MatedataRsp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 用户收货地址
 * @date 2022/2/24 16:03
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/api/address")
@Api("用户收货地址")
public class ApiAddressController {

    @Autowired
    private AddressService addressService;

    @GetMapping("/{userId}")
    @ApiOperation("根据用户id查询收货地址")
    public DefaultResponse getAddressByUserId(@PathVariable String userId){
        List<MatedataRsp> list = addressService.getAddressByUserId(userId);
        return DefaultResponse.ok(list);
    }


    @PostMapping("/add")
    @ApiOperation("添加收货地址")
    public DefaultResponse addAddress(@RequestBody AddressRequest addressRequest, HttpServletRequest request){
        addressService.addAddress(addressRequest,request);
        return DefaultResponse.ok();
    }


    @PutMapping("/edit")
    @ApiOperation("修改收货地址")
    public DefaultResponse editAddress(@RequestBody AddressRequest addressRequest){
        addressService.editAddress(addressRequest);
        return DefaultResponse.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除地址")
    public DefaultResponse deleteAddress(@PathVariable String id){
        addressService.deleteAddress(id);
        return DefaultResponse.ok();
    }

}
